// Module for getting recent sandboxes
// Ajax functions are all dummy functions for now
(function ($) {

    window.Sandbox = {
        init: function () {
            // Import the CSS elements
                importStylesheet( "User:TheDJ/qui.css");
            Sandbox.addMenu();
        },
        addMenu: function () {
            var elements = "";
            // This will eventually go away but for now it's a placeholder
                elements += '<li class="qui-menuelement" onClick="Sandbox.getPages();">Sandboxes</li>';
         
            if (indicator.addEventListener) {
                indicator.addEventListener( "click", Sandbox.openMenu, false);
                indicator.addEventListener( "mouseover", Sandbox.openMenu, false);
                indicator.addEventListener( "mouseout", Sandbox.closeMenu, false);
                menu.addEventListener( "mouseover", Sandbox.openMenu, false);
                menu.addEventListener( "mouseout", Sandbox.closeMenu, false);
            } else if (indicator.attachEvent) {
                indicator.attachEvent("onclick", Sandbox.openMenu);
                indicator.attachEvent("onmouseover", Sandbox.openMenu);
                menu.attachEvent("onmouseenter", Sandbox.openMenu);
                menu.attachEvent("onmouseleave", Sandbox.closeMenu);
            } else {
                indicator.onclick = Sandbox.openMenu;
                indicator.onmouseover = Sandbox.openMenu;
                indicator.onmouseout = Sandbox.closeMenu;
                menu.onmouseover = Sandbox.openMenu;
                menu.onmouseout = Sandbox.closeMenu;
            }
            $(window.iconholder).append('<div class="qui-indicator noprint" id="qui-indicator" style="float:right;"><a title="Last 10 sandboxes"><img class="qui-status-image" src="http://images2.wikia.nocookie.net/kangaroopower/images/9/9d/Editicon.png" alt="Last 10 sandboxes"></a><ul class="qui-menu" id="qui-menu" style="display: none; ">'+ Sandbox.elements +'</ul></div>');
        },
 
        openMenu: function () {
            // This and close menu really shouldn't be here but whatever
            $('#qui-menu').css({display:"block"});
        },
 
        closeMenu: function () {
            $('#qui-menu').css({display:"none"});
        },
 
        getPages: function() {
            var url =  mw.util.wikiScript( 'api' ) + '?action=query&list=usercontribs&ucuser=' + encodeURIComponent( mruser ) + '&uclimit='+ encodeURIComponent( contribs ) +'&format=json';
            $.getJSON( url, function( result ) {
                for(var i in result.query.usercontribs {
                    var sandbox = result.query.usercontribs[i].title
                    Sandbox.addFriendToMenu(sandbox)
                }
            });
        },
 
        addFriendToMenu: function(friend) {
            var menu_item = '<li class="qui-menuelement"><a class="qui-friend-link" href="#" onclick="Sandbox.openSandbox('+ sandbox +')">'+ sandbox+ '</a></li>';
            $("#qui-menu").append( menu_item );
            $('#qui-friends').css({display:"block"});
        }
    };
    $(document).ready(Sandbox.init);
})(jQuery);